// WndGSensorGraph.cpp : implementation file
//

#include "stdafx.h"
#include "BBViewer.h"
#include "WndGSensorGraph.h"

#include "StatusMgr.h"
#include "../_inc_arc/def_archive.h"

#define GSEN_INDICATE	45
#define GSEN_START_POS	48


// CWndGSensorGraph
IMPLEMENT_DYNAMIC(CWndGSensorGraph, CWnd)

CWndGSensorGraph::CWndGSensorGraph()
{
	m_nGraphType = GSENSOR_GRAPH_TIME_BASED;

	m_memDCGraph.m_hDC = NULL;
	m_bitmapGraph.m_hObject = NULL;

	m_pDataAll = NULL;
	m_dwDataCount = 0;
	m_dwDataSize = 0;

	m_bAvailable = FALSE;

}

CWndGSensorGraph::~CWndGSensorGraph()
{
	m_penX.DeleteObject();
	m_penY.DeleteObject();
	m_penZ.DeleteObject();
	m_penXbk.DeleteObject();
	m_penYbk.DeleteObject();
	m_penZbk.DeleteObject();
	m_penBar.DeleteObject();

	CGsensorInfo *pInfo;
	m_csGSen.Lock();
	while (m_arrGsensorInfo.GetSize())
	{
		pInfo = m_arrGsensorInfo.GetAt(0);
		delete pInfo;
		m_arrGsensorInfo.RemoveAt(0);
	}
	m_csGSen.Unlock();

	if (m_bitmapGraph.m_hObject != NULL)
		m_bitmapGraph.DeleteObject();
	m_bitmapGraph.m_hObject = NULL;

	if (m_memDCGraph.m_hDC != NULL)
		m_memDCGraph.DeleteDC();
	m_memDCGraph.m_hDC = NULL;

	if (m_pDataAll)
	{
		delete m_pDataAll;
	}
	m_pDataAll = NULL;
	m_dwDataCount = 0;
	m_dwDataSize = 0;
}


BEGIN_MESSAGE_MAP(CWndGSensorGraph, CWnd)
	ON_WM_PAINT()
	ON_WM_CREATE()
END_MESSAGE_MAP()



// CWndGSensorGraph message handlers

void CWndGSensorGraph::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// Do not call CWnd::OnPaint() for painting messages
	UpdateWnd(m_bAvailable);

}

void CWndGSensorGraph::ClearGSensor()
{
	CGsensorInfo *pInfo;
	m_csGSen.Lock();
	while (m_arrGsensorInfo.GetSize())
	{
		pInfo = m_arrGsensorInfo.GetAt(0);
		delete pInfo;
		m_arrGsensorInfo.RemoveAt(0);
	}
	m_csGSen.Unlock();

	m_csGSen.Lock();

	if (m_pDataAll != NULL)
	{
		delete m_pDataAll;
		m_pDataAll = NULL;
	}
	m_dwDataSize = 0;
	m_dwDataCount = 0;
	m_nPos = 0;

	m_rtCurRect = CRect(0, 0, 0, 0);

	m_csGSen.Unlock();

	UpdateWnd(m_bAvailable);
}


void CWndGSensorGraph::SetGSensorVal(int nX, int nY, int nZ, int nDir, BOOL bAvaiableFileBased)
{
	CGsensorInfo *pInfo;
	pInfo = new CGsensorInfo(nX, nY, nZ, nDir);

	m_csGSen.Lock();
	m_arrGsensorInfo.Add(pInfo);
	m_csGSen.Unlock();

	m_bAvailable = bAvaiableFileBased;
	UpdateWnd(bAvaiableFileBased);
}

void CWndGSensorGraph::UpdateWnd(BOOL bAvailbleFileBased /* = FALSE */)
{
	if ((m_nGraphType == GSENSOR_GRAPH_TIME_BASED)||(bAvailbleFileBased == FALSE))
		DrawGraphTimeBased();
	else
		DrawGraphFileBased();
}

void CWndGSensorGraph::DrawGraphTimeBased()
{
	CRect rect, rtGraph;
	CRect rtDiv, rtImg;
	int x, y, cx, cy;
	GetClientRect(&rect);

	CDC *pDC = GetDC();
	CDC dcBmp, dcMemory;
	CDC dcGraph;

	CBitmap *pOldBmp, *pBmp;
	CBitmap *pOldBmp2, bmp;
	CBitmap *pOldBmpBk, bmpBk;
	CPen *pOldPen;
	FLOAT fScale;
	LPST_BMP_INFO lpBmpInfo;
	LPST_WND_POS lpWndPos;

	dcMemory.CreateCompatibleDC(pDC);
	bmpBk.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	pOldBmpBk = dcMemory.SelectObject(&bmpBk);

	lpWndPos = theApp.WndPos.GetAt(wpGSensorGraphView);

	//1. Draw background
	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenGraphBG);
	if (lpBmpInfo->hbmpImage)
	{
		CDC dcBg;
		CBitmap *pBmpBg, *pOldBmpBg;

		dcBg.CreateCompatibleDC(pDC);
		pBmpBg = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
		pOldBmpBg = dcBg.SelectObject(pBmpBg);

		rtDiv = lpWndPos->rtDiv;
		rtImg = lpBmpInfo->rtImageRect;

		dcMemory.BitBlt(0, 0, rtDiv.left, rtDiv.top, &dcBg, 0, 0, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, 0, rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
			&dcBg, rtDiv.left, 0, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, 0, rtDiv.right, rtDiv.top,
			&dcBg, rtImg.Width()-rtDiv.right, 0, SRCCOPY);
		dcMemory.StretchBlt(0, rtDiv.top, rtDiv.left, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcMemory.StretchBlt(rect.Width()-rtDiv.right, rtDiv.top, rtDiv.right, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, rtImg.Width()-rtDiv.right, rtDiv.top,
			rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom),
			SRCCOPY);
		dcMemory.BitBlt(0, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, rect.Height()-rtDiv.bottom,
			rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			&dcBg, rtDiv.left, rtImg.Height()-rtDiv.bottom,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);

		dcBg.SelectObject(pOldBmpBg);
		dcBg.DeleteDC();
	}

	rtGraph = rect;
	rtGraph.DeflateRect(0, 0, 
						lpWndPos->rtStickTo.left+lpWndPos->rtStickTo.right,
						lpWndPos->rtStickTo.top+lpWndPos->rtStickTo.bottom);

	m_Y_middle = (rtGraph.Height()+1)/2;

	dcBmp.CreateCompatibleDC(pDC);
	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenGraph_bk);
	pBmp = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	pOldBmp = dcBmp.SelectObject(pBmp);

	dcGraph.CreateCompatibleDC(pDC);
	bmp.CreateCompatibleBitmap(pDC, rtGraph.Width(), rtGraph.Height());
	pOldBmp2 = dcGraph.SelectObject(&bmp);

	int cx_gsen, cy_gsen;
	CBitmap *pBmpIndi, *pOldBmpIndi;
	CDC dcIndi;
	LPST_BMP_INFO lpBmpInfoIndi = theApp.BmpInfo.GetAt(biGSenGraph_indi);
	dcIndi.CreateCompatibleDC(pDC);
	pBmpIndi = CBitmap::FromHandle(lpBmpInfoIndi->hbmpImage);
	pOldBmpIndi = dcIndi.SelectObject(pBmpIndi);

	cx_gsen = lpBmpInfo->rtImageRect.Width();
	cy_gsen = lpBmpInfo->rtImageRect.Height();

	//Drawing background
	for (y=m_Y_middle;y<rtGraph.Height();y+=cy_gsen)
	{
		cy = rtGraph.Height()-y;
		cy = (cy>=cy_gsen) ? cy_gsen : cy;
		for (x=0;x<rtGraph.Width();x+=cx_gsen)
		{
			cx = rtGraph.Width()-x;
			cx = (cx>=cx_gsen) ? cx_gsen : cx;

			dcGraph.BitBlt(x, y, cx, cy, &dcBmp, 0, 0, SRCCOPY);
			if (x==0)
			{
				dcGraph.BitBlt(0, y, lpBmpInfoIndi->rtImageRect.Width(), cy, &dcIndi, 0, 0, SRCCOPY);
			}
		}
	}

	for (y=m_Y_middle;y>-cy_gsen;y-=cy_gsen)
	{
		cy = y;
		cy = (cy>=cy_gsen) ? cy_gsen : cy;
		for (x=0;x<rtGraph.Width();x+=cx_gsen)
		{
			cx = rtGraph.Width()-x;
			cx = (cx>=cx_gsen) ? cx_gsen : cx;

			dcGraph.BitBlt(x, y-cy, cx, cy, &dcBmp, 0, 0, SRCCOPY);
			if (x==0)
			{
				dcGraph.BitBlt(0, y-cy, lpBmpInfoIndi->rtImageRect.Width(), cy, &dcIndi, 0, 0, SRCCOPY);
			}
		}
	}
	dcIndi.SelectObject(pOldBmpIndi);
	dcIndi.DeleteDC();


	CFont font, *pOldFont;

	font.CreateFontIndirect(&g_FontGpsGraph);
	pOldFont = dcGraph.SelectObject(&font);
	dcGraph.SetBkMode(TRANSPARENT);
	dcGraph.SetTextColor(m_dwColorText);

	int nHeight = rtGraph.Height()/2/cy_gsen;
	int nTemp;
	int nPos = 13;

	dcGraph.TextOut(nPos, m_Y_middle-6, _T(" 0.G"));

	if (nHeight<=2)
	{
		m_Y_2g = cy_gsen;
	}

	if (nHeight>2)
	{
		nTemp = (nHeight-1)/2*2*cy_gsen;
		dcGraph.TextOut(nPos, m_Y_middle-nTemp-6, _T(" 2.G"));
		dcGraph.TextOut(nPos, m_Y_middle+nTemp-6, _T("-2.G"));
		m_Y_2g = nTemp;
	}

	if (nHeight>4)
	{
		nTemp = ((nHeight-1)/2)*cy_gsen;
		dcGraph.TextOut(nPos, m_Y_middle-nTemp-6, _T(" 1.G"));
		dcGraph.TextOut(nPos, m_Y_middle+nTemp-6, _T("-1.G"));
		m_Y_2g = nTemp*2;
	}

	dcGraph.SelectObject(pOldFont);

	fScale = (FLOAT)(m_Y_2g/2000.);

	dcBmp.SelectObject(pOldBmp);
	dcBmp.DeleteDC();

	//////////////////////////////////////////////////////////////////////////
	//drawing graph bar

	int nBaseVPos[COORDI_COUNT];
	int nGap = rtGraph.Height()/6;
	nBaseVPos[COORDI_Y] = rtGraph.Height()/2;
	nBaseVPos[COORDI_X] = nBaseVPos[COORDI_Y] - nGap;
	nBaseVPos[COORDI_Z] = nBaseVPos[COORDI_Y] + nGap;

	int nXPos, nYPos;		
	int nMaxGsensorItem;
	int nInterval = 1;
	int i, n, nSize;
	CGsensorInfo *pInfo;

	nMaxGsensorItem = (rtGraph.Width()-GSEN_START_POS)/nInterval;
	nMaxGsensorItem -= 1;

	m_csGSen.Lock();
	nSize = m_arrGsensorInfo.GetSize();
	while (nSize>nMaxGsensorItem)
	{
		pInfo = m_arrGsensorInfo.GetAt(0);
		delete pInfo;
		m_arrGsensorInfo.RemoveAt(0);
		--nSize;
	}
	m_csGSen.Unlock();

	if (nSize>1)
	{

		//////////////////////////////////////////////////////////////////////////
		//X, Y, Z graph
		for (n=0;n<COORDI_COUNT;n++)
		{
			m_csGSen.Lock();
			if (m_arrGsensorInfo.GetSize()>1)
			{
				pInfo = m_arrGsensorInfo.GetAt(0);
				if (pInfo)
				{
					nXPos = GSEN_START_POS;
					nYPos = m_Y_middle - pInfo->GetScaled(n, fScale);
					dcGraph.MoveTo(nXPos, nYPos);

					for (i=1;i<nSize;i++)
					{
						pInfo = m_arrGsensorInfo.GetAt(i);
						pOldPen = dcGraph.SelectObject(m_pPen[pInfo->GetDir()][n]);
						nXPos += nInterval;
						nYPos = m_Y_middle - pInfo->GetScaled(n, fScale);
						nYPos = nYPos<0 ? 0 : nYPos>rtGraph.Height()-1 ? rtGraph.Height()-1 : nYPos;
						dcGraph.LineTo(nXPos, nYPos);
						dcGraph.SelectObject(pOldPen);
					}

				}
			}
			m_csGSen.Unlock();
		}

		pOldPen = dcGraph.SelectObject(&m_penBar);

		dcGraph.MoveTo(nXPos, rtGraph.top+1);
		dcGraph.LineTo(nXPos, rtGraph.bottom-1);
		dcGraph.SelectObject(pOldPen);

	}

	dcMemory.BitBlt(lpWndPos->rtStickTo.left, lpWndPos->rtStickTo.top,
					rtGraph.Width(), rtGraph.Height(),
					&dcGraph, 0, 0, SRCCOPY);

	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);
	//////////////////////////////////////////////////////////////////////////
	dcGraph.SelectObject(pOldBmp2);
	bmp.DeleteObject();
	dcGraph.DeleteDC();

	dcMemory.SelectObject(pOldBmpBk);
	bmpBk.DeleteObject();
	dcMemory.DeleteDC();

	ReleaseDC(pDC);
	
}

void CWndGSensorGraph::DrawGraphFileBased()
{
	CRect rect, rtGraph;
	GetClientRect(&rect);
	if (rect != m_rtCurRect)
		DrawTotalGSensorData();

	CDC *pDC = GetDC();
	CDC dcMem;
	CBitmap *pOldBitmap, bitmap;
	CPen *pOldPenBar;
	int nPos;

	LPST_WND_POS lpWndPos = theApp.WndPos.GetAt(wpGSensorGraphView);
	rtGraph = rect;
	rtGraph.DeflateRect(0, 0, 
		lpWndPos->rtStickTo.left+lpWndPos->rtStickTo.right,
		lpWndPos->rtStickTo.top+lpWndPos->rtStickTo.bottom);

	if (m_pDataAll)
	{
		if (m_nPos<0)
		{
			pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_memDCGraph, 0, 0, SRCCOPY);
		}
		else
		{
			dcMem.CreateCompatibleDC(pDC);
			bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
			pOldBitmap = dcMem.SelectObject(&bitmap);

			dcMem.BitBlt(0, 0, rect.Width(), rect.Height(), &m_memDCGraph, 0, 0, SRCCOPY);

			pOldPenBar = dcMem.SelectObject(&m_penBar);
			
			nPos = m_nPos*m_arrGsensorInfo.GetSize()/m_dwDataCount;
			nPos += GSEN_START_POS;

			dcMem.MoveTo(nPos, rtGraph.top+lpWndPos->rtStickTo.top+1);
			dcMem.LineTo(nPos, rtGraph.bottom+lpWndPos->rtStickTo.top-1);

			pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMem, 0, 0, SRCCOPY);

			dcMem.SelectObject(pOldBitmap);
			bitmap.DeleteObject();
			dcMem.DeleteDC();
		}
	}
	else
	{
		pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_memDCGraph, 0, 0, SRCCOPY);
	}

	ReleaseDC(pDC);
}

void CWndGSensorGraph::DrawTotalGSensorData()
{
	CDC *pDC = GetDC();
	CRect rect, rtGraph;
	CRect rtDiv, rtImg;
	int x, y, cx, cy;
	GetClientRect(&rect);

	if (m_bitmapGraph.m_hObject != NULL)
		m_bitmapGraph.DeleteObject();
	m_bitmapGraph.m_hObject = NULL;

	if (m_memDCGraph.m_hDC != NULL)
		m_memDCGraph.DeleteDC();
	m_memDCGraph.m_hDC = NULL;

	m_memDCGraph.CreateCompatibleDC(pDC);
	m_bitmapGraph.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	m_memDCGraph.SelectObject(&m_bitmapGraph);

	CDC dcBmp, dcMemory;
	CDC dcGraph;

	CBitmap *pOldBmp, *pBmp;
	CBitmap *pOldBmp2, bmp;
	CBitmap *pOldBmpBk, bmpBk;
	CPen *pOldPen;
	FLOAT fScale;
	LPST_BMP_INFO lpBmpInfo;
	LPST_WND_POS lpWndPos;


	dcMemory.CreateCompatibleDC(pDC);
	bmpBk.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
	pOldBmpBk = dcMemory.SelectObject(&bmpBk);

	lpWndPos = theApp.WndPos.GetAt(wpGSensorGraphView);

	//1. Draw background
	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenGraphBG);
	if (lpBmpInfo->hbmpImage)
	{
		CDC dcBg;
		CBitmap *pBmpBg, *pOldBmpBg;

		dcBg.CreateCompatibleDC(pDC);
		pBmpBg = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
		pOldBmpBg = dcBg.SelectObject(pBmpBg);

		rtDiv = lpWndPos->rtDiv;
		rtImg = lpBmpInfo->rtImageRect;

		dcMemory.BitBlt(0, 0, rtDiv.left, rtDiv.top, &dcBg, 0, 0, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, 0, rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.top,
			&dcBg, rtDiv.left, 0, rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.top, SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, 0, rtDiv.right, rtDiv.top,
			&dcBg, rtImg.Width()-rtDiv.right, 0, SRCCOPY);
		dcMemory.StretchBlt(0, rtDiv.top, rtDiv.left, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, 0, rtDiv.top, rtDiv.left, rtImg.Height()-(rtDiv.top+rtDiv.bottom), SRCCOPY);
		dcMemory.StretchBlt(rect.Width()-rtDiv.right, rtDiv.top, rtDiv.right, rect.Height()-(rtDiv.top+rtDiv.bottom),
			&dcBg, rtImg.Width()-rtDiv.right, rtDiv.top,
			rtDiv.right, rtImg.Height()-(rtDiv.top+rtDiv.bottom),
			SRCCOPY);
		dcMemory.BitBlt(0, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, 0, rtImg.Height()-rtDiv.bottom, SRCCOPY);
		dcMemory.StretchBlt(rtDiv.left, rect.Height()-rtDiv.bottom,
			rect.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			&dcBg, rtDiv.left, rtImg.Height()-rtDiv.bottom,
			rtImg.Width()-(rtDiv.left+rtDiv.right), rtDiv.bottom,
			SRCCOPY);
		dcMemory.BitBlt(rect.Width()-rtDiv.right, rect.Height()-rtDiv.bottom, rtDiv.left, rtDiv.bottom,
			&dcBg, rtImg.Width()-rtDiv.right, rtImg.Height()-rtDiv.bottom, SRCCOPY);

		dcBg.SelectObject(pOldBmpBg);
		dcBg.DeleteDC();
	}

	rtGraph = rect;
	rtGraph.DeflateRect(0, 0, 
		lpWndPos->rtStickTo.left+lpWndPos->rtStickTo.right,
		lpWndPos->rtStickTo.top+lpWndPos->rtStickTo.bottom);

	m_Y_middle = (rtGraph.Height()+1)/2;

	dcBmp.CreateCompatibleDC(pDC);
	lpBmpInfo = theApp.BmpInfo.GetAt(biGSenGraph_bk);
	pBmp = CBitmap::FromHandle(lpBmpInfo->hbmpImage);
	pOldBmp = dcBmp.SelectObject(pBmp);

	dcGraph.CreateCompatibleDC(pDC);
	bmp.CreateCompatibleBitmap(pDC, rtGraph.Width(), rtGraph.Height());
	pOldBmp2 = dcGraph.SelectObject(&bmp);

	int cx_gsen, cy_gsen;
	CBitmap *pBmpIndi, *pOldBmpIndi;
	CDC dcIndi;
	LPST_BMP_INFO lpBmpInfoIndi = theApp.BmpInfo.GetAt(biGSenGraph_indi);
	dcIndi.CreateCompatibleDC(pDC);
	pBmpIndi = CBitmap::FromHandle(lpBmpInfoIndi->hbmpImage);
	pOldBmpIndi = dcIndi.SelectObject(pBmpIndi);

	cx_gsen = lpBmpInfo->rtImageRect.Width();
	cy_gsen = lpBmpInfo->rtImageRect.Height();

	//Drawing background
	for (y=m_Y_middle;y<rtGraph.Height();y+=cy_gsen)
	{
		cy = rtGraph.Height()-y;
		cy = (cy>=cy_gsen) ? cy_gsen : cy;
		for (x=0;x<rtGraph.Width();x+=cx_gsen)
		{
			cx = rtGraph.Width()-x;
			cx = (cx>=cx_gsen) ? cx_gsen : cx;

			dcGraph.BitBlt(x, y, cx, cy, &dcBmp, 0, 0, SRCCOPY);
			if (x==0)
			{
				dcGraph.BitBlt(0, y, lpBmpInfoIndi->rtImageRect.Width(), cy, &dcIndi, 0, 0, SRCCOPY);
			}
		}
	}

	for (y=m_Y_middle;y>-cy_gsen;y-=cy_gsen)
	{
		cy = y;
		cy = (cy>=cy_gsen) ? cy_gsen : cy;
		for (x=0;x<rtGraph.Width();x+=cx_gsen)
		{
			cx = rtGraph.Width()-x;
			cx = (cx>=cx_gsen) ? cx_gsen : cx;

			dcGraph.BitBlt(x, y-cy, cx, cy, &dcBmp, 0, 0, SRCCOPY);
			if (x==0)
			{
				dcGraph.BitBlt(0, y-cy, lpBmpInfoIndi->rtImageRect.Width(), cy, &dcIndi, 0, 0, SRCCOPY);
			}
		}
	}
	dcIndi.SelectObject(pOldBmpIndi);
	dcIndi.DeleteDC();


	CFont font, *pOldFont;

	font.CreateFontIndirect(&g_FontGpsGraph);
	pOldFont = dcGraph.SelectObject(&font);
	dcGraph.SetBkMode(TRANSPARENT);
	dcGraph.SetTextColor(m_dwColorText);

	int nHeight = rtGraph.Height()/2/cy_gsen;
	int nTemp;
	int nPos = 13;

	dcGraph.TextOut(nPos, m_Y_middle-6, _T(" 0.G"));

	if (nHeight<=2)
	{
		m_Y_2g = cy_gsen;
	}

	if (nHeight>2)
	{
		nTemp = (nHeight-1)/2*2*cy_gsen;
		dcGraph.TextOut(nPos, m_Y_middle-nTemp-6, _T(" 2.G"));
		dcGraph.TextOut(nPos, m_Y_middle+nTemp-6, _T("-2.G"));
		m_Y_2g = nTemp;
	}

	if (nHeight>4)
	{
		nTemp = ((nHeight-1)/2)*cy_gsen;
		dcGraph.TextOut(nPos, m_Y_middle-nTemp-6, _T(" 1.G"));
		dcGraph.TextOut(nPos, m_Y_middle+nTemp-6, _T("-1.G"));
		m_Y_2g = nTemp*2;
	}

	dcGraph.SelectObject(pOldFont);

	fScale = (FLOAT)(m_Y_2g/2000.);

	dcBmp.SelectObject(pOldBmp);
	dcBmp.DeleteDC();


	//pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	//////////////////////////////////////////////////////////////////////////
	//draw data Graph
	m_csGSen.Lock();
	if (m_pDataAll && (m_dwDataCount != 0))
	{
		//////////////////////////////////////////////////////////////////////////
		//calc
		int nXPos, nYPos;		
		int nMaxGsensorItem;
		int nInterval = 1;
		int i, n, nSize;

		PPICTURE_HEADER_EX pPictureHeader;
		CGsensorInfo *pInfo;
		pPictureHeader = (PPICTURE_HEADER_EX)m_pDataAll;

		nMaxGsensorItem = (rtGraph.Width()-GSEN_START_POS)/nInterval;
		nMaxGsensorItem -= 1;

		//clear
		for (i=0;i<m_arrGsensorInfo.GetSize();i++)
		{
			pInfo = m_arrGsensorInfo.GetAt(i);
			delete pInfo;
		}
		m_arrGsensorInfo.RemoveAll();

		for (i=0;i<nMaxGsensorItem;i++)
		{
			nPos = i*m_dwDataCount/nMaxGsensorItem;
			pInfo = new CGsensorInfo((int)(pPictureHeader[nPos].gsensor_x*1000),
									 (int)(pPictureHeader[nPos].gsensor_y*1000),
									 (int)(pPictureHeader[nPos].gsensor_z*1000),
									 1);

			m_arrGsensorInfo.Add(pInfo);
		}

		nSize = m_arrGsensorInfo.GetSize();

		//////////////////////////////////////////////////////////////////////////
		//drawing graph bar
		int nBaseVPos[COORDI_COUNT];
		int nGap = rtGraph.Height()/6;
		nBaseVPos[COORDI_Y] = rtGraph.Height()/2;
		nBaseVPos[COORDI_X] = nBaseVPos[COORDI_Y] - nGap;
		nBaseVPos[COORDI_Z] = nBaseVPos[COORDI_Y] + nGap;


		if (nSize>1)
		{

			//////////////////////////////////////////////////////////////////////////
			//X, Y, Z graph
			for (n=0;n<COORDI_COUNT;n++)
			{

				if (m_arrGsensorInfo.GetSize()>1)
				{
					pInfo = m_arrGsensorInfo.GetAt(0);
					if (pInfo)
					{
						nXPos = GSEN_START_POS;
						nYPos = m_Y_middle - pInfo->GetScaled(n, fScale);
						dcGraph.MoveTo(nXPos, nYPos);

						for (i=1;i<nSize;i++)
						{
							pInfo = m_arrGsensorInfo.GetAt(i);
							pOldPen = dcGraph.SelectObject(m_pPen[pInfo->GetDir()][n]);
							nXPos += nInterval;
							nYPos = m_Y_middle - pInfo->GetScaled(n, fScale);
							nYPos = nYPos<0 ? 0 : nYPos>rtGraph.Height()-1 ? rtGraph.Height()-1 : nYPos;
							dcGraph.LineTo(nXPos, nYPos);
							dcGraph.SelectObject(pOldPen);
						}

					}
				}
			}
		}


	}
	m_csGSen.Unlock();

	dcMemory.BitBlt(lpWndPos->rtStickTo.left, lpWndPos->rtStickTo.top,
		rtGraph.Width(), rtGraph.Height(),
		&dcGraph, 0, 0, SRCCOPY);

	m_memDCGraph.BitBlt(0, 0, rect.Width(), rect.Height(), &dcMemory, 0, 0, SRCCOPY);

	//////////////////////////////////////////////////////////////////////////
	dcGraph.SelectObject(pOldBmp2);
	bmp.DeleteObject();
	dcGraph.DeleteDC();

	dcMemory.SelectObject(pOldBmpBk);
	bmpBk.DeleteObject();
	dcMemory.DeleteDC();

	ReleaseDC(pDC);

	m_rtCurRect = rect;

}

void CWndGSensorGraph::SetTotalGSensorData(BYTE *pDataAll, DWORD dwDataSize, DWORD dwDataCount)
{
	m_csGSen.Lock();
	m_dwDataSize = dwDataSize;
	m_dwDataCount = dwDataCount;

	if (m_pDataAll)
	{
		delete m_pDataAll;
		m_pDataAll = NULL;
	}
	if (m_dwDataSize)
	{
		m_pDataAll = new BYTE [m_dwDataSize];
		memcpy(m_pDataAll, pDataAll, dwDataSize);
	}
	m_rtCurRect = CRect(0, 0, 0, 0);
	m_nPos = 0;
	m_csGSen.Unlock();

	m_bAvailable = TRUE;
	UpdateWnd(TRUE);
}

void CWndGSensorGraph::SetPos(int nPos)
{
	m_nPos = nPos;
	m_bAvailable = TRUE;
	UpdateWnd(TRUE);
}
int CWndGSensorGraph::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	LPST_WND_POS lpWndPos;
	lpWndPos = theApp.WndPos.GetAt(wpGSensorGraphView);
	m_nDirection = 0;

	int nWidth = 1;
	if (lpWndPos->nMode == 1)
		nWidth = 2;
	m_penX.CreatePen(PS_SOLID, nWidth, lpWndPos->dwColorLevel[0]);
	m_penY.CreatePen(PS_SOLID, nWidth, lpWndPos->dwColorLevel[1]);
	m_penZ.CreatePen(PS_SOLID, nWidth, lpWndPos->dwColorLevel[2]);

	m_penXbk.CreatePen(PS_SOLID, nWidth, lpWndPos->dwColorLevel[3]);
	m_penYbk.CreatePen(PS_SOLID, nWidth, lpWndPos->dwColorLevel[4]);
	m_penZbk.CreatePen(PS_SOLID, nWidth, lpWndPos->dwColorLevel[5]);

	m_penBar.CreatePen(PS_SOLID, nWidth, lpWndPos->dwColorLevel[6]);

	m_dwColorText = lpWndPos->dwColorText;

	m_pPen[0][COORDI_X] = &m_penX;
	m_pPen[0][COORDI_Y] = &m_penY;
	m_pPen[0][COORDI_Z] = &m_penZ;

	m_pPen[1][COORDI_X] = &m_penXbk;
	m_pPen[1][COORDI_Y] = &m_penYbk;
	m_pPen[1][COORDI_Z] = &m_penZbk;

	return 0;
}
